package com.dc.server.controller;

import com.dc.server.entity.OrdersEntity;
import com.dc.server.entity.UserEntity;
import com.dc.server.service.IDelayedOrdersService;
import com.dc.server.service.OrdersService;
import com.dc.server.service.UserService;
import com.dc.server.util.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Random;

/**
 * @Auther: 赵聪
 * @Date: 2018/8/13 12:41
 * @Description:
 */
@RestController
public class TestController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private IDelayedOrdersService iDelayedOrdersService;

    @RequestMapping("/test")
    public String test() {
        UserEntity userEntity = userService.selectById(1);
        OrdersEntity ordersEntity = ordersService.selectById(4);
        return ordersEntity.getProduceName();
    }

    @RequestMapping("/insertOrder")
    public void insertOrder() {
        Random rand = new Random();
        for (int i = 0; i < 5; i++) {
            int expireTime = rand.nextInt(100) + 1;
            OrdersEntity ordersEntity = new OrdersEntity();
            ordersEntity.setExpireDuration(Long.valueOf(expireTime));
            ordersEntity.setOrderStatus(1);
            ordersEntity.setOrderNo(OrderNoUtil.getSerialNumber());
            ordersEntity.setAddTime(new Date());
            Date date = new Date();
            ordersEntity.setExpireTime(new Date(date.getTime() + Long.valueOf(expireTime) * 1000));
            ordersService.insert(ordersEntity);
            iDelayedOrdersService.orderDelay(ordersEntity, Long.valueOf(expireTime));
        }


    }
}
